//引入vue核心库
import Vue from 'vue'
//引入路由
import VueRouter from 'vue-router'
//调用路由插件
Vue.use(VueRouter)

//设计路由
const routes = [//设置路由
  {
    path: '/index',
    //路由的懒加载
    component: () => import('../pages/index.vue'),
    children: [ //引入二级路由 
      {
        path: '/mall',
        component: () => import('../views/mall.vue'),
        meta: {
          title: '商城'
        }
      },
      {
        path: '/class',
        component: () => import('../views/class.vue'),
        meta: {
          title: '分类'
        }
      },
      {
        path: '/scar',
        component: () => import('../views/scar.vue'),
        meta: {
          title: '购物车'
        }
      },
      {
        path: '/person',
        component: () => import('../views/person.vue'),
        meta: {
          title: '个人中心'
        }
      },
      {
        path: '',
        redirect: '/mall',
      }
    ]
  },
  {
    path: '/detail/:id',
    component: () => import('../pages/detail.vue')
  },
  {
    path: '/list/:cateid',
    component: () => import('../pages/list.vue')
  },
  {
    path: '/order',
    component: () => import('../pages/order.vue')
  },
  {
    path: '/search/:keywords',
    component: () => import('../pages/search.vue')
  },
  {
    path: '/login',
    component: () => import('../pages/login.vue')
  },
  {
    path: '/register',
    component: () => import('../pages/register.vue')
  },
  //路由的重定向
  {
    path: '*',
    redirect: '/login',
  },
]

const router = new VueRouter({
  routes,
  //路由的滚动行为
  scrollBehavior(to, from, position) {
    //用户滑动滚轴，就会产生position数据
    if (position) {
      console.log(position, '滑动位置');
      position.y = position.y + 220;
      return position;
    } else {
      return {
        x: 0,
        y: 0,
      };
    }
  },
})

//全局前置守卫
// router.beforeEach((to, from, next) => {
//   //console.log(to, "全局前置 去哪");
//   //console.log(from, "全局前置 从哪来");
//   // 1、如果它去的页面是登录 我们就next
//   if (to.path == "/login") {
//     next();
//     return;
//   }
//   // 2、如果它有了登录状态 我们就next
//   if (sessionStorage.getItem("userinfo")) {
//     next();
//     return;
//   }
//   // 3、如果以上都不符合 我们就强制它去登录
//   next("/login");
// });
export default router
